package com.yx.backstage.controller;


import com.yx.backstage.manager.DictManager;
import com.yx.backstage.model.param.DictMessageParam;
import com.yx.backstage.model.vo.DictMessageVo;
import com.yx.backstage.config.global.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author YangXiong
 * @since 2023-03-25
 */
@Slf4j
@RestController
@RequestMapping("/dict-message")
@Api(tags = "字典信息接口")
public class DictMessageController {
    @Resource
    private DictManager dictManager;

    @PostMapping("/listByType")
    @ApiOperation("根据类型获取字典列表")
    public CommonResult<List<DictMessageVo>> listByType(@RequestBody DictMessageParam param) {
        log.info("DictMessageController listByType info DictMessageParam={}", param);
        return dictManager.listByType(param.getType());
    }

    @PostMapping("/allType")
    @ApiOperation("获取所有父节点")
    public CommonResult<List<DictMessageVo>> allType() {
        return dictManager.allType();
    }

    @PostMapping("/disableDict")
    @ApiOperation("启用或者禁用子节点")
    public CommonResult<Boolean> disableDict(@RequestBody DictMessageParam param) {
        log.info("DictMessageController info disableDict param={}",param);
        Boolean result = dictManager.disableDict(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("启用或者禁用失败");
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或新增节点")
    public CommonResult<Boolean> saveOrUpdate(@RequestBody DictMessageParam param) {
        log.info("DictMessageController info saveOrUpdate param={}",param);
        Boolean result = dictManager.saveOrUpdate(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("启用或者禁用失败");
    }

    @PostMapping("/parent")
    @ApiOperation("获取父节点")
    public CommonResult<DictMessageVo> getParentByType(@RequestBody DictMessageParam param) {
        log.info("DictMessageController info getParentByType param={}",param);
        return dictManager.getParentByType(param);
    }

    @PostMapping("/delete")
    @ApiOperation("获取父节点")
    public CommonResult<Boolean> deleteDict(@RequestBody DictMessageParam param) {
        log.info("DictMessageController info deleteDict param={}",param);
        Boolean result = dictManager.deleteDict(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("删除失败");
    }
}

